
func (t *MchUserMemberCardService) AddBalance(userCardId int64, addend int) error {

	/*userCard := t.tbMchUserMemberCard.RecodeById(userCardId)

	userCardCopy := *userCard
	userCard.Balance = userCard.Balance + addend
	_, err := t.tbMchUserMemberCard.UpdateRecode(userCard)
	if err != nil {
		return err
	}*/


	var maxRetry = 10
	for maxRetry < 10 {
		maxRetry = maxRetry - 1
		isSuccess, err := func ()  (bool, error) {
			s := lego.NewDBSession()
			s.Begin()
			defer s.Close()

			userCard := t.tbMchUserMemberCard.RecodeByIdWithsession(s.GetSession(), userCardId)
			if userCard == nil {
				return false, fmt.Errorf("not found userCardId<%d>", userCardId)
			}


			userCard.Balance = userCard.Balance + addend
			count, err := t.tbMchUserMemberCard.UpdateRecodeWithSession(s.GetSession(), userCard)
			if err != nil {
				s.Rollback()
				return false, err
			}

			if  count <= 0 {
			    s.Rollback()
				return false, nil
			}


			s.Commit()
			return true, nil
		}()

		if isSuccess == true {
			return err
		}

	}

	return errors.New("AddBalance failed")
}
